package com.ming.controller;


import com.ming.pojo.Result;
import com.ming.pojo.User;
import com.ming.service.UserService;
import com.ming.utils.JwtUtil;
import com.ming.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/register")
    public Result register(@RequestBody Map<String,String> params) {
        String username = params.get("username");
        String password = params.get("password");
        //查询用户名是否已存在
        User u = userService.findByUserName(username);
        if (u == null) {
            //没有占用
            //注册
            userService.register(username, password);
            return Result.success();
        } else {
            //占用
            return Result.error("用户名已占用");
        }
    }

    @PostMapping("/login")
    public Result<String> login(@RequestBody Map<String,String> params) {
        //根据用户名查询用户
        String username = params.get("username");
        String password = params.get("password");
        User loginUser = userService.findByUserName(username);
        //判断该用户是否存在
        if (loginUser == null) {
            return Result.error("用户名不存在");
        }
        //判断密码是否正确  loginUser对象中的password是密文
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {

            //登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id",loginUser.getId());
            claims.put("username",loginUser.getUsername());
            String token = JwtUtil.genToken(claims);
            return Result.success("Bearer "+token);
        }
        return Result.error("密码错误");
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo(@RequestHeader(name = "Authorization") String token) {
        token = token.split(" ")[1];
        //根据用户名查询用户
        Map<String, Object> map = JwtUtil.parseToken(token);
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);
    }
    @PostMapping("/pwdCheck")
    public Result pwdCheck(@RequestHeader(name = "Authorization") String token, @RequestBody Map<String, String> requestBody) {
        String pwd = requestBody.get("pwd");  // 获取 JSON 中的 pwd 参数

        // 去掉 Bearer 前缀
        token = token.split(" ")[1];

        Map<String, Object> map = JwtUtil.parseToken(token);
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        if (!Md5Util.getMD5String(pwd).equals(user.getPassword())) {
            return Result.error("密码错误");
        }
        return Result.success();
    }


    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestHeader(name = "Authorization") String token,@RequestBody Map<String,String> params){
        //校验参数
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");
        // 去掉 Bearer 前缀
        token = token.split(" ")[1];

        Map<String, Object> map = JwtUtil.parseToken(token);
        String username = (String) map.get("username");
        User loginuser = userService.findByUserName(username);
        if (!loginuser.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return Result.error("旧密码错误");
        }
        if (!newPwd.equals(rePwd)){
            return Result.error("两次密码不一致");
        }
        userService.updatePwd(newPwd,loginuser.getId());
        return Result.success("修改成功");
    }

}
